import AngleUtils from "./Trigon/AngleUtils";

const { ccclass, property } = cc._decorator;

@ccclass
export default class Bullet extends cc.Component {
	@property
	speed: number = 300;

	@property
	angle: number = 0;

	rigidBody: cc.RigidBody = null;

	onLoad() {
		this.rigidBody = this.node.getComponent(cc.RigidBody);
		if (this.rigidBody == null) {
			cc.error("请添加物理属性");
		}
	}

	start() {}

	set Angle(radian: number) {
		this.angle = radian;
		this.rigidBody.linearVelocity = AngleUtils.conventToVec2(
			this.angle,
			this.speed
		);
	}

	set Speed(val: number) {
		this.speed = val;
	}

	update(dt) {
		let parent = this.node.parent;
		let pos = cc.v2(
			parent.convertToNodeSpaceAR(
				this.node.convertToWorldSpaceAR(cc.v2())
			)
		);
		if (!parent.getBoundingBox().contains(cc.v2(pos))) {
			this.node.removeFromParent();
			cc.log("destroy" + this.node.uuid);
		}
	}
  onBeginContact(contact, self: cc.Collider, other: cc.Collider) {
		if (other.node.group == "zombie"||
        other.node.group == "wall") {
          this.node.destroy();
    }
  }
}
